<template>
  <div class="container">
    <searchBox
      :total="total"
      @clearAway="clearAway"
      @onSubmit="onSubmit"
    ></searchBox>
    <el-alert
    class="elAlert"
    :title="`数据共${total}条`"
    type="info"
    show-icon>
  </el-alert>
    <el-table ref="table" :data="randerList" style="width: 100%">
      <el-table-column prop="number" label="试题编号" width="110">
      </el-table-column>
      <el-table-column prop="subject" label="学科" width="110">
      </el-table-column>
      <el-table-column prop="catalog" label="目录" width="100">
      </el-table-column>
      <el-table-column prop="questionType" label="题型" width="100">
        <template slot-scope="{ row }">
          <div>
            {{
              row.questionType === '1'
                ? '单选'
                : row.questionType === '2'
                ? '多选'
                : row.questionType === '3'
                ? '简答'
                : ''
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="question" label="题干" width="280">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="录入时间"
        width="170"
      ></el-table-column>
      <el-table-column prop="difficulty" label="难度" width="90">
        <template slot-scope="{ row }">
          <div>
            {{
              row.difficulty === '1'
                ? '简单'
                : row.difficulty === '2'
                ? '一般'
                : row.difficulty === '3'
                ? '困难'
                : ''
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="creator" label="录入人" width="90">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{row}">
          <el-button type="primary" icon="el-icon-view" circle @click="openDrawer(row.id,'preview')"></el-button>
          <el-button type="success" icon="el-icon-edit" circle @click="openDrawer(row.id,'edit')"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="openDrawer(row.id,'del')"></el-button>
          <el-button type="warning" icon="el-icon-check" circle @click="openDrawer(row.id,)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.pagesize"
        layout=" prev, pager, next,sizes, jumper"
        :total="total"
      >
      </el-pagination>
    <QuestionsPreview ref="preview"></QuestionsPreview>
  </div>
</template>

<script>
import moment from 'moment'
import QuestionsPreview from '@/module-hmmm/components/questions-preview'
import searchBox from '@/module-hmmm/components/search-box'
import { list, remove, choiceAdd } from '@/api/hmmm/questions.js'
export default {
  components: {
    searchBox,
    QuestionsPreview
  },
  data () {
    return {
      query: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      randerList: []
    }
  },
  async created () {
    this.initData()
  },
  methods: {
    clearAway () {
      this.initData()
    },
    async onSubmit (data) {
      const res = await list(data)
      this.total = res.data.counts
      this.randerList = res.data.items
      this.randerList.forEach(item => {
        const regex = /(<([^>]+)>)/gi
        item.question = item.question.replace(regex, '')
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    async openDrawer (id, type) {
      if (type === 'preview') {
        this.$refs.preview.dialogVisible = true
        this.$refs.preview.detailData(id)
      } else if (type === 'edit') {
        this.$router.push({
          path: 'new',
          query: {
            id
          }
        })
      } else if (type === 'del') {
        this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await remove({ id })
          if (this.randerList.length < 1) {
            this.query.page--
          }
          this.initData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(async () => {
          await choiceAdd({ id, choiceState: 1 })
          if (this.randerList.length < 1) {
            this.query.page--
          }
          this.initData()
          this.$message({
            type: 'success',
            message: '加入精选成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      }
    },
    // 渲染
    async initData () {
      const { data } = await list(this.query)
      this.total = data.counts
      this.randerList = data.items
      this.randerList.forEach(item => {
        const regex = /(<([^>]+)>)/gi
        item.question = item.question.replace(regex, '')
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    // 分页点击
    handleSizeChange (val) {
      this.query.pagesize = val
      this.query.page = 1
      this.initData()
    },
    handleCurrentChange (val) {
      this.query.page = val
      this.initData()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  margin: 20px;
  padding: 20px;
  background-color: #fff;
}
.container_hand {
  position: relative;
  margin-bottom: 30px;
  .container_hand_right {
    position: absolute;
    right: 0;
    width: 96px;
    height: 31px;
  }
  .container_hand_letf {
    font-size: 12px;
    color: #ffc0cb;
  }
}
.elAlert{
  margin: 10px 0;
}
</style>
